Attribute VB_Name = "HighScores"
'====================================================================================
'HighScores - Maintain and control high score rankings
'====================================================================================
'NB: This isn't the high scores screen - see 'IScreen_HighScores' instead
'    Also this isn't a class because I only ever need one instance of these variables
'    and you can't use custom defined types in a class.
Option Explicit

Public Type PM_HIGHSCORE
    CPID As String          'The unique computer identifier (so people can't steal names)
    Name As String * 3      'The high score owners' name
    Ghosts As String        'Number of ghosts eaten in total
    Time As String          'Total time spent playing
    Score As Long           'The score they achieved
    Timestamp As Date       'The date/time the score was achieved
    BuildID As String       'An identifier of the game version used. (in case a bug _
                             is found that allows cheating)
End Type

Public Type PM_HIGHSCORETABLE
    Filepath As String
    LastModified As Date        'The last time the table was modified
    LevelID As Long             '0 for playing full game, a unique ID of a level otherwise
    LevelFile As String         'Filename of the level in question
    LevelTitle As String        'Title of the level in question
    Score() As PM_HIGHSCORE    'The entries in the table
End Type
Public tScoreList As PM_HIGHSCORETABLE           'Temp list for use between classes

'====================================================================================
'HighScores_LoadScores
'====================================================================================
Public Function HighScores_LoadScores()
    'Set the size of the high score tables
    ReDim HighScores_Local.Score(19) As PM_HIGHSCORE
    
    Dim AppData As String
    AppData = SharedAPI.GetSpecialFolder(CSIDL_APPDATA)
    
    If Dir(AppData & "Camen Design", vbDirectory) = "" Then
        'Create the Camen Design folder in Application Data
        MkDir AppData & "Camen Design"
    End If
    If Dir(AppData & "Camen Design\Pac Mar", vbDirectory) = "" Then
        'Create the Pac Mar folder in Application Data
        MkDir AppData & "Camen Design\Pac Mar"
    End If
  
    'Read in the local dat file
    HighScores_LoadScoreTable AppData & "Camen Design\Pac Mar\Scores_" & PM_HS_GAMEID & ".dat", HighScores_Local
End Function

'====================================================================================
'HighScores_LoadScoreTable
'====================================================================================
Public Function HighScores_LoadScoreTable(ByVal Filepath As String, ByRef ScoreTable As PM_HIGHSCORETABLE)
    Dim K As Long
    Dim Temp As String
    
    ScoreTable.Filepath = Filepath
    
    On Local Error GoTo LblError
    Open Filepath For Input As #1
    If Err.Number = 0 Then  'If the file exists, read it in
        'Read in the file header
        Input #1, Temp
        Input #1, ScoreTable.LevelID
        Input #1, ScoreTable.LevelFile
        Input #1, ScoreTable.LevelTitle
        'Loop over each entry in the table
        For K = 0 To UBound(ScoreTable.Score)
            Input #1, ScoreTable.Score(K).BuildID
            Input #1, ScoreTable.Score(K).CPID
            Input #1, ScoreTable.Score(K).Ghosts
            Input #1, ScoreTable.Score(K).Name
            Input #1, ScoreTable.Score(K).Score
            Input #1, ScoreTable.Score(K).Time
        Next K
    End If
    
LblError:
    Close #1
    If InIDE And Err.Number <> 0 Then Stop
End Function

'====================================================================================
'HighScores_SaveScoreTable
'====================================================================================
Public Function HighScores_SaveScoreTable(ByRef ScoreTable As PM_HIGHSCORETABLE)
    On Local Error GoTo LblError
    Dim K As Long
    
    Open ScoreTable.Filepath For Output As #1
        'Write out the file header
        Print #1, "Pac Mar HighScore Table Version 1"
        Print #1, ScoreTable.LevelID
        Print #1, ScoreTable.LevelFile
        Print #1, ScoreTable.LevelTitle
        'Loop over each entry in the table
        For K = 0 To UBound(ScoreTable.Score)
            Print #1, ScoreTable.Score(K).BuildID & ", " & _
                      ScoreTable.Score(K).CPID & ", " & _
                      ScoreTable.Score(K).Ghosts & ", " & _
                      ScoreTable.Score(K).Name & ", " & _
                      ScoreTable.Score(K).Score & ", " & _
                      ScoreTable.Score(K).Time
        Next K
    
LblError:
    Close #1
    
    '!//HACK < When you get a highscore, type your name in, and then go straight to
    'the highscore screen again from the title screen, your score won't show up.
    'I think it's to do with how the damn scorelists are passed around because VB
    'Won't let me use them as function arguments. For now, just reload the file,
    'I'll fix this at another time
    HighScores.HighScores_LoadScores
End Function

'====================================================================================
'HighScores_InsertScore: Add your score to a score table
'====================================================================================
Public Function HighScores_InsertScore(ByRef ScoreTable As PM_HIGHSCORETABLE) As Long
    Dim YourRank As Long, K As Long
    
    'Calculate your Rank
    YourRank = -1
    For K = 0 To UBound(ScoreTable.Score)
        'Is your score higher?
        If Game.Score >= ScoreTable.Score(K).Score Then
            'You take this slot!
            YourRank = K: Exit For
        End If
    Next K
    'If you ranked in the table
    If YourRank > -1 Then
        'Move all the slots down one to fit you in
        For K = UBound(ScoreTable.Score) To YourRank + 1 Step -1
           With ScoreTable
           .Score(K).BuildID = .Score(K - 1).BuildID
           .Score(K).CPID = .Score(K - 1).CPID
           .Score(K).Ghosts = .Score(K - 1).Ghosts
           .Score(K).Name = .Score(K - 1).Name
           .Score(K).Score = .Score(K - 1).Score
           .Score(K).Time = .Score(K - 1).Time
           .Score(K).Timestamp = .Score(K - 1).Timestamp
            End With
        Next K
        'Insert your score and details
        With ScoreTable.Score(YourRank)
           .BuildID = Trim(VB.App.Major) & Trim(VB.App.Minor) & Trim(VB.App.Revision)
           .CPID = "1"
           .Ghosts = Game.TotalGhosts
           .Name = "   "
           .Score = Game.Score
           .Time = Game.TotalTime
           .Timestamp = Now()
        End With
    End If
        
    HighScores_InsertScore = YourRank
End Function

'=== END ============================================================================
'This work is licenced under the Creative Commons Attribution 2.5 License
